package com.ztesoft.ossup.report.service;

import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.ossup.report.dao.ChartDataRepo;
import com.ztesoft.ossup.report.dao.ChartRepository;
import com.ztesoft.ossup.report.dao.JdbcTemplateDao;
import com.ztesoft.ossup.report.dao.PieChartRepo;
import com.ztesoft.ossup.report.dao.ReportRepository;
import com.ztesoft.ossup.report.dao.ReportSubRepo;
import com.ztesoft.ossup.report.domain.ReportChart;
import com.ztesoft.ossup.report.domain.ReportSub;

@Service("chartConfigService")
public class ChartConfigService {
	@Autowired
	JdbcTemplateDao jdbcTemplateDao;

	@Autowired
	ChartRepository chartRepository;
	
	@Autowired
	ReportRepository reportRepository;
	
	@Autowired
	ChartDataRepo chartDataRepo;
	
	@Autowired
	PieChartRepo pieChartRepo;
	
	@Autowired
	ReportSubRepo reportSubRepo;
	
	@Transactional
	public Long addChart(Map<String, Object> params){
		String title = String.valueOf(params.get("title"));
		String subTitle = String.valueOf(params.get("subTitle"));
		String xAxesTitle = String.valueOf(params.get("xAxesTitle"));
		String yAxesTitle = String.valueOf(params.get("yAxesTitle"));
		String pieTitle = String.valueOf(params.get("pieTitle"));
		String borderColor = String.valueOf(params.get("borderColor"));
		String xDataKeys = String.valueOf(params.get("xDataKeys"));
		String yDataKeys = String.valueOf(params.get("yDataKeys"));
		Integer borderWidth = Integer.parseInt(String.valueOf(params.get("borderWidth")==null?"1":params.get("borderWidth")));
		ReportChart chart = new ReportChart();
		String reportId = String.valueOf(params.get("reportId"));
		reportId = reportId.replaceAll(",", "");
		chart.setReport(reportRepository.findOne(Long.parseLong(reportId)));
		chart.setTitle(title);
		chart.setSubTitle(subTitle);
		chart.setxAxesTitle(xAxesTitle);
		chart.setyAxesTitle(yAxesTitle);
		chart.setTitleForPie(pieTitle);
//		chart.setBorderColor(borderColor);
		chart.setBorderWidth(borderWidth);
		chart.setxDataKeys(xDataKeys);
		chart.setyDataKeys(yDataKeys);
		chart.state="10A";
		ReportChart result=chartRepository.save(chart);
		if (result!=null) {
			return result.id;
		}
		return 0L;
	}
	
	public List<ReportSub> getReportSub(Long reportId){
		List<ReportSub> types = reportSubRepo.findAllByReportId(reportId);
		return types;
	}
	

}
